package elite.sysfun.root.XMLBound.Run;

import com.wisdge.dataservice.Result;
import elite.core.dataBase.DBMethod;
import elite.core.dataBase.ExecResult;
import elite.core.dataBase.sys.IECDS;
import elite.core.errorManager.ErrManager;
import elite.core.root.LogObj;
import elite.core.root.PublicVar;
import elite.core.root.StaticMethod;
import elite.core.root.sqlTypeList;
import elite.core.staffInfo.OneStaff;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.jdom2.Element;

import java.io.StringReader;
import java.sql.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@Slf4j
public class OracleSubClass implements iBoundData {
    //	private final static logIF 						LoggerWrite = logCls.getMe(OracleSubClass.class);
    private Element mCurNode;
    private int mExistsPKFlag = 0;
    private String mFindFilter = "";

    private Map<String, String> allKeyValue = new HashMap<String, String>();

    //private HashMap mBigField =new HashMap();
    private String specialField(String fName) {
        if (PublicVar.getInstance().getSpecialField(fName)) {
            return "\"" + fName + "\"";
        }
        return fName;
    }

    public Element getCurNode() {
        return mCurNode;
    }

    public String getFindFilter() {
        return mFindFilter;
    }

    public int getExistsPKFlag() {
        return mExistsPKFlag;
    }

    @SuppressWarnings("rawtypes")
    public OracleSubClass(Element vCurNode, HashMap vMap) {
        mCurNode = vCurNode;
        FindKey(vMap);
    }

    @SuppressWarnings("rawtypes")
    private void FindKey(HashMap vMap) {
        int i, iCount = mCurNode.getChildren().size();
        String mTmpValue;
        StringBuffer mKeyFilter = new StringBuffer();
        for (i = 0; i < iCount; i++) {
            Element mOneNode = (Element) mCurNode.getChildren().get(i);
            if (mOneNode.getAttribute("PK") != null) {
                if (mOneNode.getAttribute("FK") != null) {
                    mTmpValue = getFKValue(vMap, mOneNode.getAttribute("FK").getValue());
                } else if (mOneNode.getAttribute("ORG") != null) {
                    mTmpValue = mOneNode.getAttribute("ORG").getValue();
                } else {
                    mTmpValue = mOneNode.getText();
                    if (mTmpValue.equalsIgnoreCase("GetNewGUID()")) {
                        mTmpValue = StaticMethod.getGUID();
                        mOneNode.setText(mTmpValue);
                    }
                }
                allKeyValue.put(mOneNode.getName(), mTmpValue);
                mExistsPKFlag = 1;
                mKeyFilter.append(" AND ");
                if (mOneNode.getAttribute("TYPE") != null) {
                    if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("int")) {
                        mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append("=").append(StaticMethod.StringToInt(mTmpValue));
                    } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("num")) {
                        mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append("=").append(StaticMethod.StringToFloat(mTmpValue));
                    } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("date")) {
                        if (mTmpValue.equals("##?#NOWDD#?##")) {
                            mTmpValue = StaticMethod.getNowTimeBySql();
                        }
                        mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append("=").append("'" + mTmpValue.replaceAll("/", "-") + "'");
                    } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("bool")) {
                        mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append("=").append(mTmpValue.replaceAll("'", "''"));
                    } else {
                        if (mTmpValue.replaceAll("'", "''").length() == 0) {
                            mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append(" is Null ");
                        } else {
                            mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append("=").append("'" + mTmpValue.replaceAll("'", "''") + "'");
                        }
                    }
                } else {
                    if (mTmpValue.replaceAll("'", "''").length() == 0) {
                        mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append(" is Null ");
                    } else {
                        mKeyFilter.append(StaticMethod.sqlTN(specialField(mOneNode.getName())).toUpperCase()).append("=").append("'" + mTmpValue.replaceAll("'", "''") + "'");
                    }
                }
            }
        }
        if (mExistsPKFlag == 1) {
            mFindFilter = mKeyFilter.toString().trim().substring(3);
        }
    }

    @SuppressWarnings("rawtypes")
    private ExecResult DeleteOneRecord(OneStaff staff, Connection vConn, String vDeleteFlag, HashMap vMap, LogObj logk) {
        Statement mStatement = DBMethod.CreateStatementUpdate(vConn);
        String StrSql = (new StringBuffer("Delete from ")).append(BoundDataExec.getTBName(staff, mCurNode.getName()).toUpperCase()).append(" Where ").append(mFindFilter).toString();
        Result mReturn = DBMethod.ExecSql(mStatement, StrSql, sqlTypeList.systemSql, logk);
        // mStatement.CLOSE_ALL_RESULTS();
        //mStatement.c .CLOSE_ALL_RESULTS();
        DBMethod.CloseStatement(mStatement);
        if (mReturn.getCode() < 0) {
            log.debug("Bound Data Delete Failed:" + StrSql);
            return new ExecResult(-1, "ExecSqlFailed", "FailedSql:" + StrSql + " Err:" + mReturn.getCode());
        } else {
            log.debug("Bound Data Delete Succ:" + mReturn + " Sql:" + StrSql);
            return new ExecResult(1, "", "");
        }
    }


    @SuppressWarnings("rawtypes")
    public ExecResult UpdateCurrentRecord(OneStaff staff, Connection vConn, HashMap vMap, String vDBType, LogObj logk) {
        if (mCurNode.getAttribute("DEL") != null) {
            return DeleteOneRecord(staff, vConn, mCurNode.getAttribute("DEL").getValue(), vMap, logk);
        } else if (mCurNode.getAttribute("NOTEDIT") != null) {
            return new ExecResult(1, "", "");
        } else if (mCurNode.getAttribute("NOTFIND") != null) {
            //    	if (mCurNode.getName().equalsIgnoreCase("Tab_OtherCmdGroup")){
            //    		System.out.println("debug");
            //    	}
            log.debug("Bound Table Not Find record:" + mCurNode.getName());
            return InsertUpdateOracleSql(0, staff, vConn, vMap, logk);
            //return InsertNoFindRecord(vConn,vMap);  //'New '
        } else {
            int notInsertFlag = 0;
            if (mCurNode.getAttribute("NI") != null) {
                notInsertFlag = 1;
            }
            log.debug("Bound Table :" + mCurNode.getName() + " Not New InsertFlag:" + notInsertFlag);
            StringBuffer mStrSql;
            if (vDBType.equalsIgnoreCase(IECDS.DB2)) {
                mStrSql = new StringBuffer("select * from ");
            } else {
                mStrSql = new StringBuffer("select RowID from ");
            }

            mStrSql.append(BoundDataExec.getTBName(staff, mCurNode.getName()).toUpperCase()).append(" where ").append(mFindFilter);
            Statement mStatement = DBMethod.CreateStatementUpdate(vConn);
            ResultSet Rec = DBMethod.getResultSet(mStatement, mStrSql.toString(), sqlTypeList.systemSql, logk);
            if (Rec == null) {
                DBMethod.CloseStatement(mStatement);
                return new ExecResult(-1, "SelectSqlFailed", mStrSql.toString());
            }
            ExecResult mResultCls;
            if (DBMethod.RecNext(Rec)) {//Update
                DBMethod.CloseResultSet(Rec);
                DBMethod.CloseStatement(mStatement);
                mResultCls = InsertUpdateOracleSql(1, staff, vConn, vMap, logk);
            } else {//New
                DBMethod.CloseResultSet(Rec);
                DBMethod.CloseStatement(mStatement);
                if (notInsertFlag == 1) {
                    mResultCls = new ExecResult(1, "", "");
                } else {
                    mResultCls = InsertUpdateOracleSql(0, staff, vConn, vMap, logk);
                }
            }
            return mResultCls;
        }
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    private ExecResult InsertUpdateOracleSql(int vFlag, OneStaff staff, Connection vConn, HashMap vMap, LogObj logk) {
        StringBuffer mFieldsInfo = new StringBuffer();
        StringBuffer mValuesInfo = new StringBuffer();
        HashMap mBigFieldInfo = new HashMap();
        int i, iCount = mCurNode.getChildren().size();
        //     if (mCurNode.getName().equalsIgnoreCase("tab_LSOrder")){
        //    	 System.out.println("test");
        //     }
        String mTmpValue = "";
        String mStrSql;
        Map<String, String> mBreakField = new HashMap<String, String>();
        if (vFlag == 0) {//Insert
            for (i = 0; i < iCount; i++) {
                Element mOneNode = (Element) mCurNode.getChildren().get(i);
                if (mOneNode.getChildren().size() > 0) {
                } else {
                    if (mOneNode.getAttribute("TYPE") != null) {
                        if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("clob")) {
                            mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase());
                            mValuesInfo.append(",empty_clob()");
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("blob")) {
                            mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase());
                            mValuesInfo.append(",empty_blob()");
                        } else {
                            mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase());
                            mValuesInfo.append(",?");
                        }
                    } else {
                        mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase());
                        mValuesInfo.append(",?");
                    }
                }
            }
            if (mFieldsInfo.length() > 1) {
                mStrSql = (new StringBuffer("Insert into ").append(BoundDataExec.getTBName(staff, mCurNode.getName()).toUpperCase()).append("(").append(mFieldsInfo.toString().substring(1)).append(") Values (").append(mValuesInfo.toString().substring(1)).append(")")).toString();
            } else {
                log.debug(logk.getLogStr() + " 无插入的字段");
                return new ExecResult(1, "", "");
            }

        } else {//Update
            for (i = 0; i < iCount; i++) {
                Element mOneNode = (Element) mCurNode.getChildren().get(i);
                if (mOneNode.getChildren().size() > 0) {
                } else if (mOneNode.getAttribute("New") != null) {
                } else {
                    if (mOneNode.getAttribute("PK") != null) {
                        if (mOneNode.getAttribute("FK") != null) {
                            mTmpValue = getFKValue(vMap, mOneNode.getAttribute("FK").getValue());
                        } else {
                            mTmpValue = mOneNode.getText();
                        }
                        if (allKeyValue.containsKey(mOneNode.getName())) {
                            if (allKeyValue.get(mOneNode.getName()).equalsIgnoreCase(mTmpValue)) {
                                mBreakField.put(mOneNode.getName(), "");
                                continue;
                            }
                        }
                    }
                    if (mOneNode.getAttribute("TYPE") != null) {
                        if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("clob")) {
                            mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase()).append("=empty_clob()");
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("blob")) {
                            mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase()).append("=empty_blob()");
                        } else {
                            mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase()).append("=?");
                        }
                    } else {
                        mFieldsInfo.append(",").append(specialField(mOneNode.getName()).toUpperCase()).append("=?");
                    }
                }
            }
            if (mFieldsInfo.length() > 1) {
                mStrSql = (new StringBuffer("Update ").append(BoundDataExec.getTBName(staff, mCurNode.getName()).toUpperCase()).append(" Set ").append(mFieldsInfo.toString().substring(1)).append(" Where ").append(mFindFilter)).toString();
            } else {
                log.debug(logk.getLogStr() + " 无更新的字段");
                return new ExecResult(1, "", "");
            }

        }
        PreparedStatement PreInsert = DBMethod.CreatePreParedStatementUpdate(vConn, mStrSql, logk);
        int mIndexInfo = 1;
        for (i = 0; i < iCount; i++) {
            Element mOneNode = (Element) mCurNode.getChildren().get(i);
            if (mOneNode.getChildren().size() > 0) {
            } else {
                if (mOneNode.getAttribute("FK") != null) {
                    mTmpValue = getFKValue(vMap, mOneNode.getAttribute("FK").getValue());
                } else {
                    mTmpValue = mOneNode.getText();
                }
                if (mOneNode.getAttribute("New") != null && vFlag != 0) {
                } else {
                    if (mBreakField.containsKey(mOneNode.getName())) {
                        continue;
                    }
                    if (mOneNode.getAttribute("TYPE") != null) {
                        if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("int")) {
                            try {
                                PreInsert.setInt(mIndexInfo, StaticMethod.StringToInt(mTmpValue));
                            } catch (Exception e) {
                                log.error(logk.getLogStr() + " Update One Rec int:Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            } catch (Error e) {
                                log.error(logk.getLogStr() + " Update One Rec SqlException int type:Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            }
                            mIndexInfo++;
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("num")) {
                            try {
                                PreInsert.setBigDecimal(mIndexInfo, StaticMethod.StringToBigDec(mTmpValue));
                                //PreInsert.setFloat(mIndexInfo,StaticMethod.StringToFloat(mTmpValue));
                            } catch (Exception e) {
                                log.error(logk.getLogStr() + " Update One Rec Number:Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            } catch (Error e) {
                                log.error(logk.getLogStr() + " Update One Rec SqlException Number type:Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            }
                            mIndexInfo++;
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("date")) {
                            Timestamp mTmp = null;
                            if (mTmpValue.trim().equalsIgnoreCase("") || mTmpValue.equalsIgnoreCase("null")) {
                            } else {
                                try {
                                    if (mTmpValue.equals("##?#NOWDD#?##")) {
                                        mTmpValue = StaticMethod.getNowTimeBySql();
                                    }
                                    mTmp = StaticMethod.ValtoSqlTimeStamp(mTmpValue, logk);
                                    //	                  	  SimpleDateFormat FormatDate ;
                                    //	  	              	  if (mTmpValue.trim().length()==10){
                                    //	  	              		  FormatDate= new SimpleDateFormat("yyyy-MM-dd");
                                    //	  	              	  }else if (mTmpValue.trim().length()==16){
                                    //							  FormatDate= new SimpleDateFormat("yyyy-MM-dd HH:mm");
                                    //	  	              	  }else if (mTmpValue.trim().length()==8){
                                    //	  	              		  FormatDate= new SimpleDateFormat("HH:mm:ss");
                                    //	  	              	  }else if (mTmpValue.trim().length()==7 || mTmpValue.trim().length()==6){
                                    //						      FormatDate= new SimpleDateFormat("yyyy-MM");
                                    //	  	              	  }else{
                                    //	  	              		  FormatDate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    //	  	              	  }
                                    //	  	                 // SimpleDateFormat FormatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    //	  	                  mTmp = new java.sql.Timestamp(FormatDate.parse(mTmpValue.replaceAll("/", "-")).getTime());
                                } catch (Exception ex2) {
                                    log.error(logk.getLogStr() + " Update One Rec :Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + ex2.toString(), ex2);
                                }
                            }
                            try {
                                PreInsert.setTimestamp(mIndexInfo, mTmp);
                            } catch (Exception e) {
                                log.error(logk.getLogStr() + " Update One Rec Sql Error - date Type :Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            }
                            mIndexInfo++;
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("bool")) {
                            boolean mTmp;
                            mTmp = Boolean.parseBoolean(mTmpValue);
                            try {
                                PreInsert.setBoolean(mIndexInfo, mTmp);
                            } catch (SQLException e) {
                                log.error(logk.getLogStr() + " Update One Rec Sql Error- boolean Type :Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            }
                            mIndexInfo++;
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("clob")) {
                            mBigFieldInfo.put("C" + mOneNode.getName(), mTmpValue);
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("blob")) {
                            mBigFieldInfo.put("B" + mOneNode.getName(), mTmpValue);
                        } else if (mOneNode.getAttribute("TYPE").getValue().equalsIgnoreCase("long")) {
                            try {
                                PreInsert.setCharacterStream(mIndexInfo, new StringReader(mTmpValue), mTmpValue.length());
                            } catch (SQLException e) {
                                log.error(logk.getLogStr() + " Update One Rec Sql Error - LONG Type:Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            }
                            mIndexInfo++;
                        } else { //String
                            if (mTmpValue.equalsIgnoreCase("GetNewGUID()")) {
                                try {
                                    String mTmpNewID = StaticMethod.getGUID();
                                    mOneNode.setText(mTmpNewID);
                                    PreInsert.setString(mIndexInfo, mTmpNewID);
                                } catch (SQLException e) {
                                    log.error(logk.getLogStr() + " Update One Rec Sql Error - String Type:Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                                }
                                mIndexInfo++;
                            } else {
                                try {
                                    //	mTmpValue=mTmpValue.replaceAll("\n","\r\n");
                                    //	int mTmpVLen=mTmpValue.length();
                                    //	if (mTmpVLen>600){
                                    //		PreInsert.setCharacterStream(mIndexInfo,new StringReader(mTmpValue), mTmpVLen);
                                    //	}else{
                                    PreInsert.setString(mIndexInfo, mTmpValue);
                                    //	                			System.out.println(" INDEX:"+mIndexInfo+"----"+mTmpValue);
                                    //	}
                                } catch (SQLException e) {
                                    log.error(logk.getLogStr() + " Update One Rec Sql Error - String Type :Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                                }
                                mIndexInfo++;
                            }
                        }
                    } else {
                        if (mTmpValue.equalsIgnoreCase("GetNewGUID()")) {
                            try {
                                String mTmpNewID = StaticMethod.getGUID();
                                mOneNode.setText(mTmpNewID);
                                PreInsert.setString(mIndexInfo, mTmpNewID);
                            } catch (SQLException e) {
                                log.error(logk.getLogStr() + " Update One Rec Sql Error - String Type :Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            }
                            mIndexInfo++;
                        } else {
                            try {
                                //mTmpValue=mTmpValue.replaceAll("\n","\r\n");
                                //                		int mTmpVLen=mTmpValue.length();
                                //                		if (mTmpVLen>600){
                                //                			PreInsert.setCharacterStream(mIndexInfo,new StringReader(mTmpValue), mTmpVLen);
                                //                		}else{
                                PreInsert.setString(mIndexInfo, mTmpValue);
                                //                			System.out.println(" INDEX:"+mIndexInfo+"----"+mTmpValue);
                                //                		}
                            } catch (SQLException e) {
                                log.error(logk.getLogStr() + " Update One Rec Sql Error - String Type :Table-" + mCurNode.getName() + " Field-" + mOneNode.getName() + " Value-" + mTmpValue + " " + e.toString(), e);
                            }
                            mIndexInfo++;
                        }
                    }
                }
            }
        }
        try {
            PreInsert.executeUpdate();
        } catch (SQLException e) {
            if (vFlag == 0) {
                log.error(logk.getLogStr() + " Insert One Rec Sql Error :Table-" + mCurNode.getName() + "   " + e.toString() + " " + mCurNode.toString(), e);
                PrintToLogOneNode(logk);
                ErrManager.getInstance().AddOneMsgSql(1, "DB", "Do Sql:" + mStrSql + " Err:" + e.toString());
                return new ExecResult(-1, String.valueOf(e.getErrorCode()), "Insert One Rec Sql Error :Table-" + mCurNode.getName() + "   " + e.toString());
            } else {
                log.error(logk.getLogStr() + " Update One Rec Sql Error :Table-" + mCurNode.getName() + "   " + e.toString() + " " + mCurNode.toString(), e);
                PrintToLogOneNode(logk);
                ErrManager.getInstance().AddOneMsgSql(1, "DB", "Do Sql:" + mStrSql + " Err:" + e.toString());
                return new ExecResult(-1, String.valueOf(e.getErrorCode()), "Update One Rec Sql Error :Table-" + mCurNode.getName() + "   " + e.toString());
            }
        }
        DBMethod.ClosePreparedStatement(PreInsert);
        if (mBigFieldInfo.size() > 0) {
            return ExecBigFieldInfo(staff, vConn, mBigFieldInfo, logk);
        } else {
            return new ExecResult(1, "", "");
        }
    }

    @SuppressWarnings("rawtypes")
    private void PrintToLogOneNode(LogObj logk) {
        List mAllSub = mCurNode.getChildren();
        for (int i = 0; i < mAllSub.size(); i++) {
            Element mTmp = (Element) mAllSub.get(i);
            log.info(logk.getLogStr() + " Print Node:" + mTmp.getName() + " Len:" + mTmp.getText().length() + "  Value:" + mTmp.getText());
        }
    }

    @SuppressWarnings("rawtypes")
    private ExecResult ExecBigFieldInfo(OneStaff staff, Connection vConn, HashMap vBigField, LogObj logk) {
        StringBuffer mFields = new StringBuffer();
        Iterator Item = vBigField.entrySet().iterator();
        while (Item.hasNext()) {
            Map.Entry mOneItem = (Map.Entry) Item.next();
            String mOneKey = (String) mOneItem.getKey();
            mFields.append(",").append(mOneKey.substring(1).toUpperCase());
        }
        StringBuffer mStrSql = new StringBuffer("select  ").append(mFields.toString().substring(1)).append(" from ");
        mStrSql.append(BoundDataExec.getTBName(staff, mCurNode.getName()).toUpperCase()).append(" where ").append(mFindFilter);//.append( "  for update ");
        Statement mStatement = DBMethod.CreateStatementUpdate(vConn);
        if (mStatement == null) {
            return new ExecResult(-1, "Open Sql Statement Failed", "");
        }
        ResultSet Rec = DBMethod.getResultSet(mStatement, mStrSql.toString(), sqlTypeList.systemSql, logk);
        if (Rec == null) {
            DBMethod.CloseStatement(mStatement);
            return new ExecResult(-1, "Open Sql Failed", mStrSql.toString());
        }
        if (DBMethod.RecNext(Rec)) {
            log.debug("Begin ExecBigFieldInfo Update:" + mStrSql);
            Iterator Item1 = vBigField.entrySet().iterator();

            StringBuilder uStrSql = new StringBuilder("update ").append(BoundDataExec.getTBName(staff, mCurNode.getName()).toUpperCase()).append(" set ");
            while (Item1.hasNext()) {
                Map.Entry mOneItem = (Map.Entry) Item1.next();
                String mOneKey = (String) mOneItem.getKey();
                String mOneValue = (String) mOneItem.getValue();
                log.debug("--CLOB类型字段mOneKey:" + mOneKey + " mOneValue:" + mOneValue);
                if (mOneKey.substring(0, 1).equals("C")) {

                    uStrSql.append(mOneKey.toString().substring(1)).append(" = '").append(StringEscapeUtils.escapeSql(mOneValue)).append("',");

                }
            }

            // OceanBase通过原始方法更新Clob字段会更新为Null的问题
            uStrSql.deleteCharAt(uStrSql.lastIndexOf(",")).append(" where ").append(mFindFilter);
            Result uReturn = DBMethod.ExecSql(mStatement, uStrSql.toString(), sqlTypeList.systemSql, logk);
            DBMethod.CloseStatement(mStatement);
            if (uReturn.getCode() < 0) {
                return new ExecResult(-1, "取CLOB类型字段 CharacterStream", uStrSql.toString());

            } else {
                log.debug("取CLOB类型字段 数据更新完成Succ:" + uReturn + " Sql:" + uStrSql);
            }


            log.info("End ExecBigFieldInfo Update:" + mStrSql);
        }
        DBMethod.CloseResultSet(Rec);
        DBMethod.CloseStatement(mStatement);
        return new ExecResult(1, "", "");
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public ExecResult ProcessSubTable(OneStaff staff, Connection vConn, HashMap vMap, String vDBType, LogObj logk) {
        vMap.put(this, "");
        int i, iCount = mCurNode.getChildren().size();
        // String mTmpValue="";
        for (i = 0; i < iCount; i++) {
            Element mOneNode = (Element) mCurNode.getChildren().get(i);
            if (mOneNode.getChildren().size() > 0) {
                OracleSubClass mTmp = new OracleSubClass(mOneNode, vMap);
                if (mTmp.getExistsPKFlag() == 1) {
                    ExecResult mResultClas = mTmp.UpdateCurrentRecord(staff, vConn, vMap, vDBType, logk);
                    if (mResultClas.getResult() < 0) {
                        return mResultClas;
                    }
                }
                ExecResult mResultClas = mTmp.ProcessSubTable(staff, vConn, vMap, vDBType, logk);
                if (mResultClas.getResult() < 0) {
                    return mResultClas;
                }
            }
        }
        vMap.remove(this);
        return new ExecResult(1, "", "");
    }

    @SuppressWarnings("rawtypes")
    private String getFKValue(HashMap vMap, String FKID) {
        String mTableName, mFieldName;
        int mIndex = FKID.indexOf(".", 0);
        if (mIndex > -1) {
            mTableName = FKID.substring(0, mIndex);
            mFieldName = FKID.substring(mIndex + 1).toUpperCase();
        } else {
            mFieldName = FKID.toUpperCase();
            mTableName = "";
        }
        Iterator mTmpAllItem = vMap.entrySet().iterator();
        while (mTmpAllItem.hasNext()) {
            Map.Entry mOneItem = (Map.Entry) mTmpAllItem.next();
            OracleSubClass mValue = (OracleSubClass) mOneItem.getKey();
            if (mTableName.equalsIgnoreCase("") || mValue.getCurNode().getName().equalsIgnoreCase(mTableName)) {
                if (mValue.getCurNode().getChild(mFieldName) != null) {
                    if (mValue.getCurNode().getChild(mFieldName).getAttribute("FK") != null) {
                        return getFKValue(vMap, mValue.getCurNode().getChild(mFieldName).getAttribute("FK").getValue());
                    } else {
                        return mValue.getCurNode().getChild(mFieldName).getText();
                    }
                }
            }
        }
        return "";
    }
}
